﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO.Ports;
//using SerialPortHelperLib;
using DevExpress.XtraCharts;
using XChip_Common;
using System.Threading;
using XChipTool.Enties;
using System.Configuration;
using BinAnalysisHelp;
using AdbService;

namespace XChipTool
{
    public partial class XChip_AWB : DevExpress.XtraEditors.XtraUserControl
    {

        public SerialPort sPortTemp;

        private bool isRead = false;

        private long[] m_nSum;
        private bool isWrite = false;
        private string radiostr = string.Empty;

        private addressregisterManager aBll;
        private List<addressregister> XChipAwb;
        private KeyEventArgs keyEnter = new KeyEventArgs(Keys.Enter);//用来手动触发文本框下发配置
        //记录是否使用USB设备读写
        public bool IsUsbOpen = false;
        //记录是否使用I2C设备读写
        public bool m_open = false;
        // 使用服务定位器获取ADB服务
        private IAdbCommandService adbService = ServiceLocator.AdbCommandService;
        public XChip_AWB()
        {
            InitializeComponent();
        }
        public XChip_AWB(SerialPort _sPortTemp,addressregisterManager _aBll)
        {
               aBll = _aBll;
               XChipAwb = aBll.GetList(c => c.ChipType == ConfigurationManager.AppSettings["xc151_ChipType"] && (c.Classify == "awb" || c.Classify == "Common"));
            radiostr = "_long";
            InitializeComponent();
        }

        private Control[] Controls_Find(string str)
        {
            //var t =   this.Controls.Find(type, true);
            List<Control> listc = new List<Control>();
            System.Reflection.FieldInfo[] fieldInfo = this.GetType().GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            for (int i = 0; i < fieldInfo.Length; i++)
            {
                if (fieldInfo[i].Name.ToLower().Contains( str.ToLower()))
                {
                    listc.AddRange(this.Controls.Find(fieldInfo[i].Name, true));
                    //break;
                }
            }
            return listc.ToArray();
        }


        private async void simpleButton5_Click(object sender, EventArgs e)
        {
            WaitBefore.Start("读取中", "正在加载中", 10);
            isRead = true;

            List<string> NameList = new List<string>();

            // 记录开始时间和寄存器计数
            var startTime = DateTime.Now;
            int totalRegisterCount = 0;
            for (int i = 0; i < XChipAwb.Count; i++)
            {
                if (XChipAwb[i].NameType.Contains("BLC_value"))
                {

                }
                var type = AppHelper.NameTypeToString(XChipAwb[i].NameType);
                if (NameList.Exists(c => c.ToLower() == (type)))
                {
                    continue;
                }
                ////加上单选框的_en
                //if (XChipAwb[i].ControlType == "1")
                //{
                //    type = XChipAwb[i].NameType;
                //}
                var col = Controls_Find(type);

                foreach (var item in col)
                {
                    if (item is TextEdit)
                    {

                        var nList = XChipAwb.Where(c => c.NameType.ToLower().Contains(item.Name.ToString().ToLower()) && c.NameType.ToLower().Contains(radiostr) && c.ControlType == "0").ToList();
                        if (nList.Count == 0)
                        {
                            nList = XChipAwb.Where(c => c.NameType.ToLower().Contains(item.Name.ToString().ToLower()) && c.ControlType == "0").ToList();
                        }
                        if (nList.Count == 1)
                        {
                            // 单个寄存器读取
                            var address = nList[0].Address;
                            totalRegisterCount++;
                            await ReadRegisterAndUpdateTextAsync(address, item as TextEdit, nList[0].Bits, nList[0].Hex);
                        }
                        else
                        {
                            // 多个寄存器组合读取
                            totalRegisterCount += nList.Count;
                            await ReadMultipleRegistersAndUpdateTextAsync(nList, item as TextEdit);
                        }
                    }
                    else if (item is CheckEdit)
                    {
                        var nList = XChipAwb.Where(c => c.NameType.ToLower().Contains(item.Name.ToString().ToLower()) && c.ControlType == "1").ToList();
                        if (nList.Count == 1)
                        {
                            var address = nList[0].Address;
                            totalRegisterCount++;
                            await ReadRegisterAndUpdateCheckAsync(address, item as CheckEdit, nList[0].Bits, nList[0].Hex);
                        }
                    }

                    NameList.Add(item.Name);
                }

                //NameList.Add(type);
            }

            isRead = false;
            //绘图暂时关闭
            ImageSetPoint(true);
            WaitBefore.Close();
        }
        void RegMapModule(long addr, long value)
        {

            switch (addr)
            {
                case 0x0730: startX_win0.Text = value.ToString("X2"); break;
                case 0x0731: endX_win0.Text = value.ToString("X2"); break;
                case 0x0732: startY_win0.Text = value.ToString("X2"); break;
                case 0x0733: endY_win0.Text = value.ToString("X2"); break;
                case 0x0734: startX_win1.Text = value.ToString("X2"); break;
                case 0x0735: endX_win1.Text = value.ToString("X2"); break;
                case 0x0736: startY_win1.Text = value.ToString("X2"); break;
                case 0x0737: endY_win1.Text = value.ToString("X2"); break;
                case 0x0738: startX_win2.Text = value.ToString("X2"); break;
                case 0x0739: endX_win2.Text = value.ToString("X2"); break;
                case 0x073a: startY_win2.Text = value.ToString("X2"); break;
                case 0x073b: endY_win2.Text = value.ToString("X2"); break;
                case 0x073c: startX_win3.Text = value.ToString("X2"); break;
                case 0x073d: endX_win3.Text = value.ToString("X2"); break;
                case 0x073e: startY_win3.Text = value.ToString("X2"); break;
                case 0x073f: endY_win3.Text = value.ToString("X2"); break;
                case 0x0740: startX_win4.Text = value.ToString("X2"); break;
                case 0x0741: endX_win4.Text = value.ToString("X2"); break;
                case 0x0742: startY_win4.Text = value.ToString("X2"); break;
                case 0x0743: endY_win4.Text = value.ToString("X2"); break;
                case 0x0744: startX_win5.Text = value.ToString("X2"); break;
                case 0x0745: endX_win5.Text = value.ToString("X2"); break;
                case 0x0746: startY_win5.Text = value.ToString("X2"); break;
                case 0x0747: endY_win5.Text = value.ToString("X2"); break;
                case 0x0748: startX_win6.Text = value.ToString("X2"); break;
                case 0x0749: endX_win6.Text = value.ToString("X2"); break;
                case 0x074a: startY_win6.Text = value.ToString("X2"); break;
                case 0x074b: endY_win6.Text = value.ToString("X2"); break;
                case 0x074c: startX_win7.Text = value.ToString("X2"); break;
                case 0x074d: endX_win7.Text = value.ToString("X2"); break;
                case 0x074e: startY_win7.Text = value.ToString("X2"); break;
                case 0x074f: endY_win7.Text = value.ToString("X2"); break;

               
                default:
                    break;
            }
        }

       

        private async void simpleButton2_Click(object sender, EventArgs e)
        {
            var H = (await GetAWBRegister($"Cur_Rgain_H{radiostr}")).ToString("X2");
            var L = (await GetAWBRegister($"Cur_Rgain_L{radiostr}")).ToString("X2");
            Cur_Rgain.Text = H + L;
        }

        private async void simpleButton3_Click(object sender, EventArgs e)
        {
            var H = (await GetAWBRegister($"Cur_Bgian_H{radiostr}")).ToString("X2");
            var L = (await GetAWBRegister($"Cur_Bgian_L{radiostr}")).ToString("X2");
            Cur_Bgian.Text = H + L;
            
        }

        
        XChipAwbChart graphEdit;
        Color boardColor = Color.Black;
        private long sum_x;
        private long sum_y;

        #region 初始化窗体
        private void XChip_AWB_Load(object sender, EventArgs e)
        {
            //ImageSetPoint(true, sum_x, sum_y);
            Task.Run(() => {
                this.Invoke((EventHandler)delegate {

                    graphEdit = new XChipAwbChart(300, 300, boardColor);
                    graphEdit.HorizontalMargin = 20;                                   //横水平边距
                    graphEdit.VerticalMargin = 20;                                     //竖垂直边距
                    graphEdit.AreasColor = Color.FromArgb(100, 0, 0, 0);         //画图区域颜色
                    graphEdit.GraphColor = Color.YellowGreen;        //曲线面积颜色
                    graphEdit.AxisColor = Color.FromArgb(255, 255, 255);         //坐标轴颜色
                    graphEdit.ScaleColor = Color.IndianRed;          //刻度线颜色

                    graphEdit.XScaleCount = 8;          //X轴刻度线数量
                    graphEdit.YScaleCount = 8;          //Y轴刻度线数量
                    List<XChipPoint> data = new List<XChipPoint>();
                    //data.Add(new XChipPoint() { Img_StartX = 5, Img_StartY = 6, Img_EndX = 50, Img_EndY = 60 });
                    //data.Add(new XChipPoint() { Img_StartX = 31, Img_StartY = 31, Img_EndX = 93, Img_EndY = 124 });
                    Image img = graphEdit.GetCurrentGraph(data, 256, 256, true);
                    pictureEdit1.Invoke((EventHandler)delegate { pictureEdit1.Image = img; });

                    List<string> NameList = new List<string>();
                    for (int i = 0; i < XChipAwb.Count; i++)
                    {

                        var type = AppHelper.NameTypeToString(XChipAwb[i].NameType);
                        //加上单选框的_en
                        if (XChipAwb[i].ControlType == "1")
                        {
                            type = XChipAwb[i].NameType;
                        }
                        if (NameList.Exists(c => c.Contains(type))) continue;
                        NameList.Add(type);
                        try
                        {
                            var col = this.Controls.Find(type, true);


                            foreach (var item in col)
                            {   //Control col = this.Controls.Find(type, true)[0];
                                if (item is TextEdit)
                                {
                                    //声明一个SuerToolTip
                                    DevExpress.Utils.SuperToolTip graduationBatchSuperToolTip = new DevExpress.Utils.SuperToolTip();
                                    //声明一个ToolTipTitleItem,保存标题
                                    DevExpress.Utils.ToolTipTitleItem toolTipTitleItem = new DevExpress.Utils.ToolTipTitleItem();
                                    String tooltips = "";

                                    if (!string.IsNullOrEmpty(XChipAwb[i].Bit_Wide))
                                    {
                                        tooltips += $"位宽={XChipAwb[i].Bit_Wide}";
                                    }
                                    if (!string.IsNullOrEmpty(XChipAwb[i].Bit_Accuracy))
                                    {
                                        tooltips += $"|精度={XChipAwb[i].Bit_Accuracy}";
                                    }
                                    //设置标题文字
                                    toolTipTitleItem.Text = tooltips;
                                    //toolTipTitleItem.Invoke(); 
                                    //把标题添加到SuerToolTip
                                    graduationBatchSuperToolTip.Items.Add(toolTipTitleItem);
                                    //指定控件的SuperTip

                                    ((TextEdit)item).SuperTip = graduationBatchSuperToolTip;
                                    //正则判断
                                    ((TextEdit)item).Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.RegEx;
                                    ((TextEdit)item).Properties.Mask.EditMask = BitsHelp.GetBitsToMask(XChipAwb[i].Bits, XChipAwb[i].Bit_Wide, XChipAwb.Where(c => c.NameType.ToLower().Contains(type) && c.ControlType == "0").Select(c => c.Bits).ToList());
                                }
                               
                            }
                       
                        }
                        catch (Exception ex)
                        {
                            continue;
                        }
                    }
                });
            });
            
            //BTN_AWB_SUM.Enabled = false;
            m_nSum = new long[2];//初始化
            //AWB_radiomode.SelectedIndex = 0;
            //radiostr = "long";


        }
        #endregion

        #region 白平衡统计窗口 -- 窗体
        /// <summary>
        /// 窗口1 竖列发送数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        /// <summary>
        /// 这个是重新画图
        /// </summary>
        private void ImageSetPoint(bool isbool,long x = 0,long y= 0)
        {

            pictureEdit1.Image = null;
            //pictureEdit1.Image.Dispose(); //释放资源文件  

            graphEdit = new XChipAwbChart(300, 300, boardColor);
            graphEdit.HorizontalMargin = 20;                                   //横水平边距
            graphEdit.VerticalMargin = 20;                                     //竖垂直边距
            graphEdit.AreasColor = Color.FromArgb(100, 0, 0, 0);         //画图区域颜色
            graphEdit.GraphColor = Color.YellowGreen;        //曲线面积颜色
            graphEdit.AxisColor = Color.FromArgb(255, 255, 255);         //坐标轴颜色
            graphEdit.ScaleColor = Color.IndianRed;          //刻度线颜色

            if(sum_x!=0 && sum_y != 0)
            {
                graphEdit.sum_y = sum_y;
                graphEdit.sum_x = sum_x;
            }
            else
            {
                graphEdit.sum_y = y;
                graphEdit.sum_x = x;
            }           

            graphEdit.XScaleCount = 8;          //X轴刻度线数量
            graphEdit.YScaleCount = 8;          //Y轴刻度线数量
            List<XChipPoint> data = new List<XChipPoint>();
            if (!string.IsNullOrEmpty(startX_win0.Text) && !string.IsNullOrEmpty(startY_win0.Text) && !string.IsNullOrEmpty(endX_win0.Text) && !string.IsNullOrEmpty(endY_win0.Text) && !string.IsNullOrEmpty(weight_win0.Text) && (Convert.ToUInt16( weight_win0.Text,16)!=0))
            {
                data.Add(new XChipPoint() { Img_StartX = Convert.ToInt32(startX_win0.Text, 16), Img_StartY = Convert.ToInt32(startY_win0.Text, 16), Img_EndX = Convert.ToInt32(endX_win0.Text, 16), Img_EndY = Convert.ToInt32(endY_win0.Text, 16),Img_Num="1" });
                if (!string.IsNullOrEmpty(startX_win1.Text) && !string.IsNullOrEmpty(startY_win1.Text) && !string.IsNullOrEmpty(endX_win1.Text) && !string.IsNullOrEmpty(endY_win1.Text) && (Convert.ToUInt16(weight_win1.Text, 16) != 0))
                {
                    data.Add(new XChipPoint() { Img_StartX = Convert.ToInt32(startX_win1.Text, 16), Img_StartY = Convert.ToInt32(startY_win1.Text, 16), Img_EndX = Convert.ToInt32(endX_win1.Text, 16), Img_EndY = Convert.ToInt32(endY_win1.Text, 16), Img_Num = "2" });
                    if (!string.IsNullOrEmpty(startX_win2.Text) && !string.IsNullOrEmpty(startY_win2.Text) && !string.IsNullOrEmpty(endX_win2.Text) && !string.IsNullOrEmpty(endY_win2.Text) && (Convert.ToUInt16(weight_win2.Text, 16) != 0))
                    {
                        data.Add(new XChipPoint() { Img_StartX = Convert.ToInt32(startX_win2.Text, 16), Img_StartY = Convert.ToInt32(startY_win2.Text, 16), Img_EndX = Convert.ToInt32(endX_win2.Text, 16), Img_EndY = Convert.ToInt32(endY_win2.Text, 16), Img_Num = "3" });
                        if (!string.IsNullOrEmpty(startX_win3.Text) && !string.IsNullOrEmpty(startY_win3.Text) && !string.IsNullOrEmpty(endX_win3.Text) && !string.IsNullOrEmpty(endY_win3.Text) && (Convert.ToUInt16(weight_win3.Text, 16) != 0))
                        {
                            data.Add(new XChipPoint() { Img_StartX = Convert.ToInt32(startX_win3.Text, 16), Img_StartY = Convert.ToInt32(startY_win3.Text, 16), Img_EndX = Convert.ToInt32(endX_win3.Text, 16), Img_EndY = Convert.ToInt32(endY_win3.Text, 16), Img_Num = "4" });
                            if (!string.IsNullOrEmpty(startX_win4.Text) && !string.IsNullOrEmpty(startY_win4.Text) && !string.IsNullOrEmpty(endX_win4.Text) && !string.IsNullOrEmpty(endY_win4.Text) && (Convert.ToUInt16(weight_win4.Text, 16) != 0))
                            {
                                data.Add(new XChipPoint() { Img_StartX = Convert.ToInt32(startX_win4.Text, 16), Img_StartY = Convert.ToInt32(startY_win4.Text, 16), Img_EndX = Convert.ToInt32(endX_win4.Text, 16), Img_EndY = Convert.ToInt32(endY_win4.Text, 16), Img_Num = "5" });
                                if (!string.IsNullOrEmpty(startX_win5.Text) && !string.IsNullOrEmpty(startY_win5.Text) && !string.IsNullOrEmpty(endX_win5.Text) && !string.IsNullOrEmpty(endY_win5.Text) && (Convert.ToUInt16(weight_win5.Text, 16) != 0))
                                {
                                    data.Add(new XChipPoint() { Img_StartX = Convert.ToInt32(startX_win5.Text, 16), Img_StartY = Convert.ToInt32(startY_win5.Text, 16), Img_EndX = Convert.ToInt32(endX_win5.Text, 16), Img_EndY = Convert.ToInt32(endY_win5.Text, 16), Img_Num = "6" });
                                    if (!string.IsNullOrEmpty(startX_win6.Text) && !string.IsNullOrEmpty(startY_win6.Text) && !string.IsNullOrEmpty(endX_win6.Text) && !string.IsNullOrEmpty(endY_win6.Text) && (Convert.ToUInt16(weight_win6.Text, 16) != 0))
                                    {
                                        data.Add(new XChipPoint() { Img_StartX = Convert.ToInt32(startX_win6.Text, 16), Img_StartY = Convert.ToInt32(startY_win6.Text, 16), Img_EndX = Convert.ToInt32(endX_win6.Text, 16), Img_EndY = Convert.ToInt32(endY_win6.Text, 16), Img_Num = "7" });
                                        if (!string.IsNullOrEmpty(startX_win7.Text) && !string.IsNullOrEmpty(startY_win7.Text) && !string.IsNullOrEmpty(endX_win7.Text) && !string.IsNullOrEmpty(endY_win7.Text) && (Convert.ToUInt16(weight_win7.Text, 16) != 0))
                                        {
                                            data.Add(new XChipPoint() { Img_StartX = Convert.ToInt32(startX_win7.Text, 16), Img_StartY = Convert.ToInt32(startY_win7.Text, 16), Img_EndX = Convert.ToInt32(endX_win7.Text, 16), Img_EndY = Convert.ToInt32(endY_win7.Text, 16), Img_Num = "8" });
                                            
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (!string.IsNullOrEmpty(redlight_win_start_x.Text) && !string.IsNullOrEmpty(redlight_win_start_y.Text) && !string.IsNullOrEmpty(redlight_win_end_x.Text) && !string.IsNullOrEmpty(redlight_win_end_y.Text))
            {
                data.Add(new XChipPoint() { isColor = false, Img_StartX = Convert.ToInt32(redlight_win_start_x.Text, 16), Img_StartY = Convert.ToInt32(redlight_win_start_y.Text, 16), Img_EndX = Convert.ToInt32(redlight_win_end_x.Text, 16), Img_EndY = Convert.ToInt32(redlight_win_end_y.Text, 16), Img_Num = "0" });
            }
            /****这里只写到文本框 窗口9 后面的未添加 暂时不用 现在先不写****/
            Image img = graphEdit.GetCurrentGraph(data, 256, 256, true);
            pictureEdit1.Image = img;
        }
        #endregion


       

       

        private async void awbChecked_ItemCheck(object sender, DevExpress.XtraEditors.Controls.ItemCheckEventArgs e)
        {

            switch (Convert.ToUInt16(awbChecked.Items[e.Index].Value))
            {
                case 1:
                    {
                        //调试模式
                        if (e.State == CheckState.Checked)
                        {
                            //BTN_AWB_SUM.Enabled = true;
                            //long nValue = SerialSend.ReadRegister(Convert.ToUInt32(XChipAwb.Where(c => c.NameType == "坐标模式").First().Address, 16), sPortTemp);
                            long nValue = await GetAWBRegister($"坐标模式{radiostr}");
                            if (XChipAwb.Exists(c => c.NameType.ToLower() == $"坐标模式{radiostr}".ToLower()))
                            {
                                var model = XChipAwb.Find(c => c.NameType.ToLower() == $"坐标模式{radiostr}".ToLower());
                                //Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                                SetAWBRegister(model, (ulong)(nValue | 0x10));
                            }
                            //SetAWBRegister("坐标模式", (ulong)(nValue | 0x10));
                        }
                        else
                        {
                            //BTN_AWB_SUM.Enabled = false;
                            //long nValue = SerialSend.ReadRegister(Convert.ToUInt32(XChipAwb.Where(c => c.NameType == "坐标模式").First().Address, 16), sPortTemp);
                            long nValue = await GetAWBRegister($"坐标模式{radiostr}");
                            //SetAWBRegister("坐标模式", (ulong)(nValue & 0xEF));
                            if (XChipAwb.Exists(c => c.NameType.ToLower() == $"坐标模式{radiostr}".ToLower()))
                            {
                                var model = XChipAwb.Find(c => c.NameType.ToLower() == $"坐标模式{radiostr}".ToLower());
                                //Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                                SetAWBRegister(model, (ulong)(nValue & 0xEF));
                            }
                        }
                    } break;
                case 0:
                    {

                        //坐标模式
                        if (e.State == CheckState.Checked)
                        {
                            //long nValue = SerialSend.ReadRegister(Convert.ToUInt32(XChipAwb.Where(c => c.NameType == "坐标模式").First().Address, 16), sPortTemp);
                            long nValue = await GetAWBRegister($"坐标模式{radiostr}");
                            //SetAWBRegister("坐标模式", (ulong)(nValue | 0x80));
                            if (XChipAwb.Exists(c => c.NameType.ToLower() == $"坐标模式{radiostr}".ToLower()))
                            {
                                var model = XChipAwb.Find(c => c.NameType.ToLower() == $"坐标模式{radiostr}".ToLower());
                                //Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                                SetAWBRegister(model, (ulong)(nValue | 0x80));
                            }
                            if (!string.IsNullOrEmpty(startX_win0.Text) && !string.IsNullOrEmpty(startY_win0.Text) && !string.IsNullOrEmpty(endX_win0.Text) && !string.IsNullOrEmpty(endY_win0.Text))
                            {
                                var tempx = (int)(Convert.ToInt32(startX_win0.Text, 16) * 0.5);
                                var tempy = (int)(Convert.ToInt32(startY_win0.Text, 16) * 0.5);
                                var tempex = (int)(Convert.ToInt32(endX_win0.Text, 16) * 0.5);
                                var tempey = (int)(Convert.ToInt32(endY_win0.Text, 16) * 0.5);
                                startX_win0.Text = tempx < 0 ? "0" : tempx.ToString("X2");
                                startY_win0.Text = tempy < 0 ? "0" : tempy.ToString("X2");
                                endX_win0.Text = tempex < 0 ? "0" : tempex.ToString("X2");
                                endY_win0.Text = tempey < 0 ? "0" : tempey.ToString("X2");
                            }
                            if (!string.IsNullOrEmpty(startX_win1.Text) && !string.IsNullOrEmpty(startY_win1.Text) && !string.IsNullOrEmpty(endX_win1.Text) && !string.IsNullOrEmpty(endY_win1.Text))
                            {
                                var tempx = (int)(Convert.ToInt32(startX_win1.Text, 16) * 0.5);
                                var tempy = (int)(Convert.ToInt32(startY_win1.Text, 16) * 0.5);
                                var tempex = (int)(Convert.ToInt32(endX_win1.Text, 16) * 0.5);
                                var tempey = (int)(Convert.ToInt32(endY_win1.Text, 16) * 0.5);
                                startX_win1.Text = tempx < 0 ? "0" : tempx.ToString("X2");
                                startY_win1.Text = tempy < 0 ? "0" : tempy.ToString("X2");
                                endX_win1.Text = tempex < 0 ? "0" : tempex.ToString("X2");
                                endY_win1.Text = tempey < 0 ? "0" : tempey.ToString("X2");
                            }
                            if (!string.IsNullOrEmpty(startX_win2.Text) && !string.IsNullOrEmpty(startY_win2.Text) && !string.IsNullOrEmpty(endX_win2.Text) && !string.IsNullOrEmpty(endY_win2.Text))
                            {
                                var tempx = (int)(Convert.ToInt32(startX_win2.Text, 16) * 0.5);
                                var tempy = (int)(Convert.ToInt32(startY_win2.Text, 16) * 0.5);
                                var tempex = (int)(Convert.ToInt32(endX_win2.Text, 16) * 0.5);
                                var tempey = (int)(Convert.ToInt32(endY_win2.Text, 16) * 0.5);
                                startX_win2.Text = tempx < 0 ? "0" : tempx.ToString("X2");
                                startY_win2.Text = tempy < 0 ? "0" : tempy.ToString("X2");
                                endX_win2.Text = tempex < 0 ? "0" : tempex.ToString("X2");
                                endY_win2.Text = tempey < 0 ? "0" : tempey.ToString("X2");

                            }
                            if (!string.IsNullOrEmpty(startX_win3.Text) && !string.IsNullOrEmpty(startY_win3.Text) && !string.IsNullOrEmpty(endX_win3.Text) && !string.IsNullOrEmpty(endY_win3.Text))
                            {
                                var tempx = (int)(Convert.ToInt32(startX_win3.Text, 16) * 0.5);
                                var tempy = (int)(Convert.ToInt32(startY_win3.Text, 16) * 0.5);
                                var tempex = (int)(Convert.ToInt32(endX_win3.Text, 16) * 0.5);
                                var tempey = (int)(Convert.ToInt32(endY_win3.Text, 16) * 0.5);
                                startX_win3.Text = tempx < 0 ? "0" : tempx.ToString("X2");
                                startY_win3.Text = tempy < 0 ? "0" : tempy.ToString("X2");
                                endX_win3.Text = tempex < 0 ? "0" : tempex.ToString("X2");
                                endY_win3.Text = tempey < 0 ? "0" : tempey.ToString("X2");

                            }
                            if (!string.IsNullOrEmpty(startX_win4.Text) && !string.IsNullOrEmpty(startY_win4.Text) && !string.IsNullOrEmpty(endX_win4.Text) && !string.IsNullOrEmpty(endY_win4.Text))
                            {
                                var tempx = (int)(Convert.ToInt32(startX_win4.Text, 16) * 0.5);
                                var tempy = (int)(Convert.ToInt32(startY_win4.Text, 16) * 0.5);
                                var tempex = (int)(Convert.ToInt32(endX_win4.Text, 16) * 0.5);
                                var tempey = (int)(Convert.ToInt32(endY_win4.Text, 16) * 0.5);
                                startX_win4.Text = tempx < 0 ? "0" : tempx.ToString("X2");
                                startY_win4.Text = tempy < 0 ? "0" : tempy.ToString("X2");
                                endX_win4.Text = tempex < 0 ? "0" : tempex.ToString("X2");
                                endY_win4.Text = tempey < 0 ? "0" : tempey.ToString("X2");


                            }
                            if (!string.IsNullOrEmpty(startX_win5.Text) && !string.IsNullOrEmpty(startY_win5.Text) && !string.IsNullOrEmpty(endX_win5.Text) && !string.IsNullOrEmpty(endY_win5.Text))
                            {
                                var tempx = (int)(Convert.ToInt32(startX_win5.Text, 16) * 0.5);
                                var tempy = (int)(Convert.ToInt32(startY_win5.Text, 16) * 0.5);
                                var tempex = (int)(Convert.ToInt32(endX_win5.Text, 16) * 0.5);
                                var tempey = (int)(Convert.ToInt32(endY_win5.Text, 16) * 0.5);
                                startX_win5.Text = tempx < 0 ? "0" : tempx.ToString("X2");
                                startY_win5.Text = tempy < 0 ? "0" : tempy.ToString("X2");
                                endX_win5.Text = tempex < 0 ? "0" : tempex.ToString("X2");
                                endY_win5.Text = tempey < 0 ? "0" : tempey.ToString("X2");

                            }
                            if (!string.IsNullOrEmpty(startX_win6.Text) && !string.IsNullOrEmpty(startY_win6.Text) && !string.IsNullOrEmpty(endX_win6.Text) && !string.IsNullOrEmpty(endY_win6.Text))
                            {
                                var tempx = (int)(Convert.ToInt32(startX_win6.Text, 16) * 0.5);
                                var tempy = (int)(Convert.ToInt32(startY_win6.Text, 16) * 0.5);
                                var tempex = (int)(Convert.ToInt32(endX_win6.Text, 16) * 0.5);
                                var tempey = (int)(Convert.ToInt32(endY_win6.Text, 16) * 0.5);
                                startX_win6.Text = tempx < 0 ? "0" : tempx.ToString("X2");
                                startY_win6.Text = tempy < 0 ? "0" : tempy.ToString("X2");
                                endX_win6.Text = tempex < 0 ? "0" : tempex.ToString("X2");
                                endY_win6.Text = tempey < 0 ? "0" : tempey.ToString("X2");

                            }
                            if (!string.IsNullOrEmpty(startX_win7.Text) && !string.IsNullOrEmpty(startY_win7.Text) && !string.IsNullOrEmpty(endX_win7.Text) && !string.IsNullOrEmpty(endY_win7.Text))
                            {
                                var tempx = (int)(Convert.ToInt32(startX_win7.Text, 16) * 0.5);
                                var tempy = (int)(Convert.ToInt32(startY_win7.Text, 16) * 0.5);
                                var tempex = (int)(Convert.ToInt32(endX_win7.Text, 16) * 0.5);
                                var tempey = (int)(Convert.ToInt32(endY_win7.Text, 16) * 0.5);
                                startX_win7.Text = tempx < 0 ? "0" : tempx.ToString("X2");
                                startY_win7.Text = tempy < 0 ? "0" : tempy.ToString("X2");
                                endX_win7.Text = tempex < 0 ? "0" : tempex.ToString("X2");
                                endY_win7.Text = tempey < 0 ? "0" : tempey.ToString("X2");
                            }
                        }
                        else
                        {
                            //long nValue = SerialSend.ReadRegister(Convert.ToUInt32(XChipAwb.Where(c => c.NameType == "坐标模式").First().Address, 16), sPortTemp);
                            long nValue = await GetAWBRegister($"坐标模式{radiostr}");
                            //SetAWBRegister("坐标模式", (ulong)(nValue & 0x7F));
                            if (XChipAwb.Exists(c => c.NameType.ToLower() == $"坐标模式{radiostr}".ToLower()))
                            {
                                var model = XChipAwb.Find(c => c.NameType.ToLower() == $"坐标模式{radiostr}".ToLower());
                                //Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                                SetAWBRegister(model, (ulong)(nValue & 0x7F));
                            }
                            if (!string.IsNullOrEmpty(startX_win0.Text) && !string.IsNullOrEmpty(startY_win0.Text) && !string.IsNullOrEmpty(endX_win0.Text) && !string.IsNullOrEmpty(endY_win0.Text))
                            {
                                var tempx = (int)(Convert.ToInt32(startX_win0.Text, 16) * 2);
                                var tempy = (int)(Convert.ToInt32(startY_win0.Text, 16) * 2);
                                var tempex = (int)(Convert.ToInt32(endX_win0.Text, 16) * 2);
                                var tempey = (int)(Convert.ToInt32(endY_win0.Text, 16) * 2);
                                startX_win0.Text = tempx > 255 ? "FF" : tempx.ToString("X2");
                                startY_win0.Text = tempy > 255 ? "FF" : tempy.ToString("X2");
                                endX_win0.Text = tempex > 255 ? "FF" : tempex.ToString("X2");
                                endY_win0.Text = tempey > 255 ? "FF" : tempey.ToString("X2");
                            }
                            if (!string.IsNullOrEmpty(startX_win1.Text) && !string.IsNullOrEmpty(startY_win1.Text) && !string.IsNullOrEmpty(endX_win1.Text) && !string.IsNullOrEmpty(endY_win1.Text))
                            {
                                var tempx = (int)(Convert.ToInt32(startX_win1.Text, 16) * 2);
                                var tempy = (int)(Convert.ToInt32(startY_win1.Text, 16) * 2);
                                var tempex = (int)(Convert.ToInt32(endX_win1.Text, 16) * 2);
                                var tempey = (int)(Convert.ToInt32(endY_win1.Text, 16) * 2);
                                startX_win1.Text = tempx > 255 ? "FF" : tempx.ToString("X2");
                                startY_win1.Text = tempy > 255 ? "FF" : tempy.ToString("X2");
                                endX_win1.Text = tempex > 255 ? "FF" : tempex.ToString("X2");
                                endY_win1.Text = tempey > 255 ? "FF" : tempey.ToString("X2");
                            }
                            if (!string.IsNullOrEmpty(startX_win2.Text) && !string.IsNullOrEmpty(startY_win2.Text) && !string.IsNullOrEmpty(endX_win2.Text) && !string.IsNullOrEmpty(endY_win2.Text))
                            {
                                var tempx = (int)(Convert.ToInt32(startX_win2.Text, 16) * 2);
                                var tempy = (int)(Convert.ToInt32(startY_win2.Text, 16) * 2);
                                var tempex = (int)(Convert.ToInt32(endX_win2.Text, 16) * 2);
                                var tempey = (int)(Convert.ToInt32(endY_win2.Text, 16) * 2);
                                startX_win2.Text = tempx > 255 ? "FF" : tempx.ToString("X2");
                                startY_win2.Text = tempy > 255 ? "FF" : tempy.ToString("X2");
                                endX_win2.Text = tempex > 255 ? "FF" : tempex.ToString("X2");
                                endY_win2.Text = tempey > 255 ? "FF" : tempey.ToString("X2");

                            }
                            if (!string.IsNullOrEmpty(startX_win3.Text) && !string.IsNullOrEmpty(startY_win3.Text) && !string.IsNullOrEmpty(endX_win3.Text) && !string.IsNullOrEmpty(endY_win3.Text))
                            {
                                var tempx = (int)(Convert.ToInt32(startX_win3.Text, 16) * 2);
                                var tempy = (int)(Convert.ToInt32(startY_win3.Text, 16) * 2);
                                var tempex = (int)(Convert.ToInt32(endX_win3.Text, 16) * 2);
                                var tempey = (int)(Convert.ToInt32(endY_win3.Text, 16) * 2);
                                startX_win3.Text = tempx > 255 ? "FF" : tempx.ToString("X2");
                                startY_win3.Text = tempy > 255 ? "FF" : tempy.ToString("X2");
                                endX_win3.Text = tempex > 255 ? "FF" : tempex.ToString("X2");
                                endY_win3.Text = tempey > 255 ? "FF" : tempey.ToString("X2");

                            }
                            if (!string.IsNullOrEmpty(startX_win4.Text) && !string.IsNullOrEmpty(startY_win4.Text) && !string.IsNullOrEmpty(endX_win4.Text) && !string.IsNullOrEmpty(endY_win4.Text))
                            {
                                var tempx = (int)(Convert.ToInt32(startX_win4.Text, 16) * 2);
                                var tempy = (int)(Convert.ToInt32(startY_win4.Text, 16) * 2);
                                var tempex = (int)(Convert.ToInt32(endX_win4.Text, 16) * 2);
                                var tempey = (int)(Convert.ToInt32(endY_win4.Text, 16) * 2);
                                startX_win4.Text = tempx > 255 ? "FF" : tempx.ToString("X2");
                                startY_win4.Text = tempy > 255 ? "FF" : tempy.ToString("X2");
                                endX_win4.Text = tempex > 255 ? "FF" : tempex.ToString("X2");
                                endY_win4.Text = tempey > 255 ? "FF" : tempey.ToString("X2");


                            }
                            if (!string.IsNullOrEmpty(startX_win5.Text) && !string.IsNullOrEmpty(startY_win5.Text) && !string.IsNullOrEmpty(endX_win5.Text) && !string.IsNullOrEmpty(endY_win5.Text))
                            {
                                var tempx = (int)(Convert.ToInt32(startX_win5.Text, 16) * 2);
                                var tempy = (int)(Convert.ToInt32(startY_win5.Text, 16) * 2);
                                var tempex = (int)(Convert.ToInt32(endX_win5.Text, 16) * 2);
                                var tempey = (int)(Convert.ToInt32(endY_win5.Text, 16) * 2);
                                startX_win5.Text = tempx > 255 ? "FF" : tempx.ToString("X2");
                                startY_win5.Text = tempy > 255 ? "FF" : tempy.ToString("X2");
                                endX_win5.Text = tempex > 255 ? "FF" : tempex.ToString("X2");
                                endY_win5.Text = tempey > 255 ? "FF" : tempey.ToString("X2");

                            }
                            if (!string.IsNullOrEmpty(startX_win6.Text) && !string.IsNullOrEmpty(startY_win6.Text) && !string.IsNullOrEmpty(endX_win6.Text) && !string.IsNullOrEmpty(endY_win6.Text))
                            {
                                var tempx = (int)(Convert.ToInt32(startX_win6.Text, 16) * 2);
                                var tempy = (int)(Convert.ToInt32(startY_win6.Text, 16) * 2);
                                var tempex = (int)(Convert.ToInt32(endX_win6.Text, 16) * 2);
                                var tempey = (int)(Convert.ToInt32(endY_win6.Text, 16) * 2);
                                startX_win6.Text = tempx > 255 ? "FF" : tempx.ToString("X2");
                                startY_win6.Text = tempy > 255 ? "FF" : tempy.ToString("X2");
                                endX_win6.Text = tempex > 255 ? "FF" : tempex.ToString("X2");
                                endY_win6.Text = tempey > 255 ? "FF" : tempey.ToString("X2");

                            }
                            if (!string.IsNullOrEmpty(startX_win7.Text) && !string.IsNullOrEmpty(startY_win7.Text) && !string.IsNullOrEmpty(endX_win7.Text) && !string.IsNullOrEmpty(endY_win7.Text))
                            {
                                var tempx = (int)(Convert.ToInt32(startX_win7.Text, 16) * 2);
                                var tempy = (int)(Convert.ToInt32(startY_win7.Text, 16) * 2);
                                var tempex = (int)(Convert.ToInt32(endX_win7.Text, 16) * 2);
                                var tempey = (int)(Convert.ToInt32(endY_win7.Text, 16) * 2);
                                startX_win7.Text = tempx > 255 ? "FF" : tempx.ToString("X2");
                                startY_win7.Text = tempy > 255 ? "FF" : tempy.ToString("X2");
                                endX_win7.Text = tempex > 255 ? "FF" : tempex.ToString("X2");
                                endY_win7.Text = tempey > 255 ? "FF" : tempey.ToString("X2");
                            }
                        }
                    } break;
                default:
                    break;
            }
           


        }

       
        /// <summary>
        /// 分量统计
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void BTN_AWB_SUM_Click(object sender, EventArgs e)
        {
           
            long nSum0, nSum8, nSum16, nSum24;
            long nBSum, nGbSum, nGrSum, nRSum;

            nSum24 = await GetAWBRegister($"分量统计_B0{radiostr}");
            nSum16 = await GetAWBRegister($"分量统计_B1{radiostr}");
            nSum8 = await GetAWBRegister($"分量统计_B2{radiostr}");
            nSum0 = await GetAWBRegister($"分量统计_B3{radiostr}");
            nBSum = nSum0 << 24 | nSum8 << 16 | nSum16 << 8 | nSum24;

            nSum24 = await GetAWBRegister($"分量统计_Gb0{radiostr}");
            nSum16 = await GetAWBRegister($"分量统计_Gb1{radiostr}");
            nSum8 = await GetAWBRegister($"分量统计_Gb2{radiostr}");
            nSum0 = await GetAWBRegister($"分量统计_Gb3{radiostr}");
            nGbSum = nSum0 << 24 | nSum8 << 16 | nSum16 << 8 | nSum24;

            nSum24 = await GetAWBRegister($"分量统计_R0{radiostr}");
            nSum16 = await GetAWBRegister($"分量统计_R1{radiostr}");
            nSum8 = await GetAWBRegister($"分量统计_R2{radiostr}");
            nSum0 = await GetAWBRegister($"分量统计_R3{radiostr}");
            nRSum = nSum0 << 24 | nSum8 << 16 | nSum16 << 8 | nSum24;

            nSum24 = await GetAWBRegister($"分量统计_Gr0{radiostr}");
            nSum16 = await GetAWBRegister($"分量统计_Gr1{radiostr}");
            nSum8 = await GetAWBRegister($"分量统计_Gr2{radiostr}");
            nSum0 = await GetAWBRegister($"分量统计_Gr3{radiostr}");
            nGrSum = nSum0 << 24 | nSum8 << 16 | nSum16 << 8 | nSum24;

            if (nBSum > 0 && nRSum > 0)
            {
                sum_x = nGbSum * 64 / nBSum;
                sum_y = nGrSum * 64 / nRSum;

                if (awbChecked.Items[0].CheckState == CheckState.Checked)
                {
                    sum_x = (long)(sum_x * 0.5);
                    sum_y = (long)(sum_y * 0.5);
                }
            }
            
            bool isbool = awbChecked.Items[0].CheckState == CheckState.Checked;
            //重新画图
            ImageSetPoint(isbool, sum_x, sum_y);
        }
        private async Task<long> GetAWBRegister(string nameType)
        {
            if (XChipAwb.Where(c => c.NameType.ToLower() == nameType.ToLower()).Count() > 0)
            {
                long var = await ReadRegisterAsync(XChipAwb.Where(c => c.NameType.ToLower() == nameType.ToLower()).First().Address);
                return var;
            }
            else
            {
                DevExpress.XtraEditors.XtraMessageBox.Show($"未找到{nameType}"); return -1;
            }
        }

        private async Task SetAWBRegister(addressregister model, ulong add,bool isCheck = true)
        {
            if (isRead) return;

            bool success = await WriteRegisterAsync(model.Address, add.ToString("X"));

            if (!success)
            {
                Console.WriteLine($"[ADBForm] SetAWBRegister 写入失败 - 地址: {model.Address}, 值: {add:X}");
            }
        }

        private async void AWB_BLC_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (isRead) return; //判断是否在读，读的时候不进行写           
            if (string.IsNullOrEmpty(BLC_value.Text)) return;
            //SetAWBRegister("BLC_value", (ulong)Convert.ToInt32(BLC_value.Text, 16));
            if (XChipAwb.Exists(c => c.NameType.ToLower() == $"BLC_value{radiostr}".ToLower()))
            {
                var model = XChipAwb.Find(c => c.NameType.ToLower() == $"BLC_value{radiostr}".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, BLC_value.Text);
                SetAWBRegister(model, Val);
            }
            switch (await ReadRegisterAsync("20000EDD"))
            {
                case 0:
                    if (XChipAwb.Exists(c => c.NameType.ToLower() == $"BLC_value_day{radiostr.Replace("_","")}".ToLower()))
                    {
                        var model = XChipAwb.Find(c => c.NameType.ToLower() == $"BLC_value_day{radiostr.Replace("_", "")}".ToLower());
                        var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, BLC_value.Text);
                        SetAWBRegister(model, Val);
                    }
                    break;
                case 1:
                    if (XChipAwb.Exists(c => c.NameType.ToLower() == $"BLC_value_night{radiostr.Replace("_", "")}".ToLower()))
                    {
                        var model = XChipAwb.Find(c => c.NameType.ToLower() == $"BLC_value_night{radiostr.Replace("_", "")}".ToLower());
                        var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, BLC_value.Text);
                        SetAWBRegister(model, Val);
                    }
                    break;
                case 2:
                    if (XChipAwb.Exists(c => c.NameType.ToLower() == $"BLC_value_midnight{radiostr.Replace("_", "")}".ToLower()))
                    {
                        var model = XChipAwb.Find(c => c.NameType.ToLower() == $"BLC_value_midnight{radiostr.Replace("_", "")}".ToLower());
                        var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, BLC_value.Text);
                        SetAWBRegister(model, Val);
                    }
                    break;
            }
            
        }

        private void AWB_MAXVALUE_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (isRead) return; //判断是否在读，读的时候不进行写           
            if (string.IsNullOrEmpty(Max_value.Text)) return;
           
                if (XChipAwb.Exists(c => c.NameType.ToLower() == $"Max_value_H{radiostr}".ToLower()))
                {
                    var model = XChipAwb.Find(c => c.NameType.ToLower() == $"Max_value_H{radiostr}".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Max_value.Text);
                    SetAWBRegister(model, Val);
                }
                if (XChipAwb.Exists(c => c.NameType.ToLower() == $"Max_value_L{radiostr}".ToLower()))
                {
                    var model = XChipAwb.Find(c => c.NameType.ToLower() == $"Max_value_L{radiostr}".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Max_value.Text);
                    SetAWBRegister(model, Val);
                }
                
        }

        private void AWB_MINVALUE_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (isRead) return; //判断是否在读，读的时候不进行写           
            if (string.IsNullOrEmpty(Min_value.Text)) return;
            if ((Convert.ToInt32(Min_value.Text, 16) > 0x3ff))
            {
                //SetAWBRegister("Min_value_H", 0x03);
                if (XChipAwb.Exists(c => c.NameType.ToLower() == $"Min_value_H{radiostr}".ToLower()))
                {
                    var model = XChipAwb.Find(c => c.NameType.ToLower() == $"Min_value_H{radiostr}".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, "3ff");
                    SetAWBRegister(model, Val);
                }
                //SetAWBRegister("Min_value_L", 0xff);
                if (XChipAwb.Exists(c => c.NameType.ToLower() == $"Min_value_L{radiostr}".ToLower()))
                {
                    var model = XChipAwb.Find(c => c.NameType.ToLower() == $"Min_value_L{radiostr}".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, "3ff");
                    SetAWBRegister(model, Val);
                }
                isRead = true;
                Min_value.Text = "3FF";
                isRead = false;
            }
            else
            {
                if (XChipAwb.Exists(c => c.NameType.ToLower() == $"Min_value_H{radiostr}".ToLower()))
                {
                    var model = XChipAwb.Find(c => c.NameType.ToLower() == $"Min_value_H{radiostr}".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Min_value.Text);
                    SetAWBRegister(model, Val);
                }
                if (XChipAwb.Exists(c => c.NameType.ToLower() == $"Min_value_L{radiostr}".ToLower()))
                {
                    var model = XChipAwb.Find(c => c.NameType.ToLower() == $"Min_value_L{radiostr}".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Min_value.Text);
                    SetAWBRegister(model, Val);
                }
                
            }
        }

        private void IMG1_STARTX_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (isRead)//判断是否在读，读的时候不进行写
            {
                return;
            }
            var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            if (string.IsNullOrEmpty(txtEdit.Text)) return;

            if (XChipAwb.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}{radiostr}".ToLower()))
            {
                var model = XChipAwb.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}{radiostr}".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetAWBRegister(model, Val);
            }
            bool isbool = awbChecked.Items[0].CheckState == CheckState.Checked;
            //重新画图
            ImageSetPoint(isbool);
        }

        private void IMG2_STARTX_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (isRead)//判断是否在读，读的时候不进行写
            {
                return;
            }
            var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            int tagNumber = 1;
            if (string.IsNullOrEmpty(txtEdit.Text)) return;
            ulong Val = 0;
            switch (txtEdit.Tag.ToString())
            {
                case "0":
                    {
                        if (XChipAwb.Exists(c => c.NameType.ToLower() == $"startX_win{tagNumber}{radiostr}".ToLower()))
                        {
                            var model = XChipAwb.Find(c => c.NameType.ToLower() == $"startX_win{tagNumber}{radiostr}".ToLower());
                            Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                            SetAWBRegister(model, Val);
                        }

                    }
                    break;
                case "1":
                    {
                        if (XChipAwb.Exists(c => c.NameType.ToLower() == $"startY_win{tagNumber}{radiostr}".ToLower()))
                        {
                            var model = XChipAwb.Find(c => c.NameType.ToLower() == $"startY_win{tagNumber}{radiostr}".ToLower());
                            Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                            SetAWBRegister(model, Val);
                        }

                    }
                    break;
                case "2":
                    {
                        if (XChipAwb.Exists(c => c.NameType.ToLower() == $"endX_win{tagNumber}{radiostr}".ToLower()))
                        {
                            var model = XChipAwb.Find(c => c.NameType.ToLower() == $"endX_win{tagNumber}{radiostr}".ToLower());
                            Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                            SetAWBRegister(model, Val);
                        }

                    }
                    break;
                case "3":
                    {
                        if (XChipAwb.Exists(c => c.NameType.ToLower() == $"endY_win{tagNumber}{radiostr}".ToLower()))
                        {
                            var model = XChipAwb.Find(c => c.NameType.ToLower() == $"endY_win{tagNumber}{radiostr}".ToLower());
                            Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                            SetAWBRegister(model, Val);
                        }

                    }
                    break;
                case "4":
                    {
                        if (string.IsNullOrEmpty(weight_win0.Text)) return;//两个框组成一个寄存器，所以下发时还需判断另一个框是否为空
                        if (XChipAwb.Exists(c => c.NameType.ToLower() == $"weight_win0{radiostr}".ToLower()))
                        {
                            var model = XChipAwb.Find(c => c.NameType.ToLower() == $"weight_win0{radiostr}".ToLower());
                            SetAWBRegister(model, Convert.ToUInt32($"{ weight_win0.Text}{weight_win1.Text}", 16));
                        }
                    }
                    break;
                default:
                    break;
            }
            bool isbool = awbChecked.Items[0].CheckState == CheckState.Checked;
            //重新画图
            ImageSetPoint(isbool);
        }

        private void IMG3_STARTX_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (isRead)//判断是否在读，读的时候不进行写
            {
                return;
            }
            var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            int tagNumber = 2;
            if (string.IsNullOrEmpty(txtEdit.Text)) return;
            ulong Val = 0;
            switch (txtEdit.Tag.ToString())
            {
                case "0":
                    {
                        if (XChipAwb.Exists(c => c.NameType.ToLower() == $"startX_win{tagNumber}{radiostr}".ToLower()))
                        {
                            var model = XChipAwb.Find(c => c.NameType.ToLower() == $"startX_win{tagNumber}{radiostr}".ToLower());
                            Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                            SetAWBRegister(model, Val);
                        }

                    }
                    break;
                case "1":
                    {
                        if (XChipAwb.Exists(c => c.NameType.ToLower() == $"startY_win{tagNumber}{radiostr}".ToLower()))
                        {
                            var model = XChipAwb.Find(c => c.NameType.ToLower() == $"startY_win{tagNumber}{radiostr}".ToLower());
                            Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                            SetAWBRegister(model, Val);
                        }

                    }
                    break;
                case "2":
                    {
                        if (XChipAwb.Exists(c => c.NameType.ToLower() == $"endX_win{tagNumber}{radiostr}".ToLower()))
                        {
                            var model = XChipAwb.Find(c => c.NameType.ToLower() == $"endX_win{tagNumber}{radiostr}".ToLower());
                            Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                            SetAWBRegister(model, Val);
                        }

                    }
                    break;
                case "3":
                    {
                        if (XChipAwb.Exists(c => c.NameType.ToLower() == $"endY_win{tagNumber}{radiostr}".ToLower()))
                        {
                            var model = XChipAwb.Find(c => c.NameType.ToLower() == $"endY_win{tagNumber}{radiostr}".ToLower());
                            Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                            SetAWBRegister(model, Val);
                        }

                    }
                    break;
                case "4":
                    {
                        if (string.IsNullOrEmpty(weight_win3.Text)) return;//两个框组成一个寄存器，所以下发时还需判断另一个框是否为空

                        if (XChipAwb.Exists(c => c.NameType.ToLower() == $"weight_win2{radiostr}".ToLower()))
                        {
                            var model = XChipAwb.Find(c => c.NameType.ToLower() == $"weight_win2{radiostr}".ToLower());
                            SetAWBRegister(model, Convert.ToUInt32($"{ weight_win2.Text}{weight_win3.Text}", 16));
                        }

                    }
                    break;
                default:
                    break;
            }
            bool isbool = awbChecked.Items[0].CheckState == CheckState.Checked;
            //重新画图
            ImageSetPoint(isbool);
        }

        private void IMG4_STARTX_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (isRead)//判断是否在读，读的时候不进行写
            {
                return;
            }
            var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            int tagNumber = 3;
            if (string.IsNullOrEmpty(txtEdit.Text)) return;
            ulong Val = 0;
            switch (txtEdit.Tag.ToString())
            {
                case "0":
                    {
                        if (XChipAwb.Exists(c => c.NameType.ToLower() == $"startX_win{tagNumber}{radiostr}".ToLower()))
                        {
                            var model = XChipAwb.Find(c => c.NameType.ToLower() == $"startX_win{tagNumber}{radiostr}".ToLower());
                            Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                            SetAWBRegister(model, Val);
                        }

                    }
                    break;
                case "1":
                    {
                        if (XChipAwb.Exists(c => c.NameType.ToLower() == $"startY_win{tagNumber}{radiostr}".ToLower()))
                        {
                            var model = XChipAwb.Find(c => c.NameType.ToLower() == $"startY_win{tagNumber}{radiostr}".ToLower());
                            Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                            SetAWBRegister(model, Val);
                        }

                    }
                    break;
                case "2":
                    {
                        if (XChipAwb.Exists(c => c.NameType.ToLower() == $"endX_win{tagNumber}{radiostr}".ToLower()))
                        {
                            var model = XChipAwb.Find(c => c.NameType.ToLower() == $"endX_win{tagNumber}{radiostr}".ToLower());
                            Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                            SetAWBRegister(model, Val);
                        }

                    }
                    break;
                case "3":
                    {
                        if (XChipAwb.Exists(c => c.NameType.ToLower() == $"endY_win{tagNumber}{radiostr}".ToLower()))
                        {
                            var model = XChipAwb.Find(c => c.NameType.ToLower() == $"endY_win{tagNumber}{radiostr}".ToLower());
                            Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                            SetAWBRegister(model, Val);
                        }

                    }
                    break;
                case "4":
                    {
                        if (string.IsNullOrEmpty(weight_win2.Text)) return;//两个框组成一个寄存器，所以下发时还需判断另一个框是否为空
                        if (XChipAwb.Exists(c => c.NameType.ToLower() == $"weight_win2{radiostr}".ToLower()))
                        {
                            var model = XChipAwb.Find(c => c.NameType.ToLower() == $"weight_win2{radiostr}".ToLower());
                            SetAWBRegister(model, Convert.ToUInt32($"{ weight_win2.Text}{weight_win3.Text}", 16));
                        }

                    }
                    break;
                default:
                    break;
            }
            bool isbool = awbChecked.Items[0].CheckState == CheckState.Checked;
            //重新画图
            ImageSetPoint(isbool);
        }

        private void IMG5_STARTX_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (isRead)//判断是否在读，读的时候不进行写
            {
                return;
            }
            var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            int tagNumber = 4;
            if (string.IsNullOrEmpty(txtEdit.Text)) return;
            ulong Val = 0;
            switch (txtEdit.Tag.ToString())
            {
                case "0":
                    {
                        if (XChipAwb.Exists(c => c.NameType.ToLower() == $"startX_win{radiostr}{tagNumber}".ToLower()))
                        {
                            var model = XChipAwb.Find(c => c.NameType.ToLower() == $"startX_win{radiostr}{tagNumber}".ToLower());
                            Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                            SetAWBRegister(model, Val);
                        }

                    }
                    break;
                case "1":
                    {
                        if (XChipAwb.Exists(c => c.NameType.ToLower() == $"startY_win{radiostr}{tagNumber}".ToLower()))
                        {
                            var model = XChipAwb.Find(c => c.NameType.ToLower() == $"startY_win{radiostr}{tagNumber}".ToLower());
                            Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                            SetAWBRegister(model, Val);
                        }

                    }
                    break;
                case "2":
                    {
                        if (XChipAwb.Exists(c => c.NameType.ToLower() == $"endX_win{radiostr}{tagNumber}".ToLower()))
                        {
                            var model = XChipAwb.Find(c => c.NameType.ToLower() == $"endX_win{radiostr}{tagNumber}".ToLower());
                            Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                            SetAWBRegister(model, Val);
                        }

                    }
                    break;
                case "3":
                    {
                        if (XChipAwb.Exists(c => c.NameType.ToLower() == $"endY_win{radiostr}{tagNumber}".ToLower()))
                        {
                            var model = XChipAwb.Find(c => c.NameType.ToLower() == $"endY_win{radiostr}{tagNumber}".ToLower());
                            Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                            SetAWBRegister(model, Val);
                        }

                    }
                    break;
                case "4":
                    {
                        if (string.IsNullOrEmpty(weight_win5.Text)) return;//两个框组成一个寄存器，所以下发时还需判断另一个框是否为空
                        if (XChipAwb.Exists(c => c.NameType.ToLower() == $"weight_win{radiostr}4".ToLower()))
                        {
                            var model = XChipAwb.Find(c => c.NameType.ToLower() == $"weight_win{radiostr}4".ToLower());
                            SetAWBRegister(model, Convert.ToUInt32($"{ weight_win4.Text}{weight_win5.Text}", 16));
                        }

                    }
                    break;
                default:
                    break;
            }
            bool isbool = awbChecked.Items[0].CheckState == CheckState.Checked;
            //重新画图
            ImageSetPoint(isbool);
        }

        private void IMG6_STARTX_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (isRead)//判断是否在读，读的时候不进行写
            {
                return;
            }
            var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            int tagNumber = 5;
            if (string.IsNullOrEmpty(txtEdit.Text)) return;
            ulong Val = 0;
            switch (txtEdit.Tag.ToString())
            {
                case "0":
                    {
                        if (XChipAwb.Exists(c => c.NameType.ToLower() == $"endY_win{radiostr}{tagNumber}".ToLower()))
                        {
                            var model = XChipAwb.Find(c => c.NameType.ToLower() == $"endY_win{radiostr}{tagNumber}".ToLower());
                            Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                            SetAWBRegister(model, Val);
                        }

                    }
                    break;
                case "1":
                    {
                        if (XChipAwb.Exists(c => c.NameType.ToLower() == $"startY_win{radiostr}{tagNumber}".ToLower()))
                        {
                            var model = XChipAwb.Find(c => c.NameType.ToLower() == $"startY_win{radiostr}{tagNumber}".ToLower());
                            Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                            SetAWBRegister(model, Val);
                        }

                    }
                    break;
                case "2":
                    {
                        if (XChipAwb.Exists(c => c.NameType.ToLower() == $"endX_win{radiostr}{tagNumber}".ToLower()))
                        {
                            var model = XChipAwb.Find(c => c.NameType.ToLower() == $"endX_win{radiostr}{tagNumber}".ToLower());
                            Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                            SetAWBRegister(model, Val);
                        }

                    }
                    break;
                case "3":
                    {
                        if (XChipAwb.Exists(c => c.NameType.ToLower() == $"endY_win{radiostr}{tagNumber}".ToLower()))
                        {
                            var model = XChipAwb.Find(c => c.NameType.ToLower() == $"endY_win{radiostr}{tagNumber}".ToLower());
                            Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                            SetAWBRegister(model, Val);
                        }

                    }
                    break;
                case "4":
                    {
                        if (string.IsNullOrEmpty(weight_win4.Text)) return;//两个框组成一个寄存器，所以下发时还需判断另一个框是否为空
                        if (XChipAwb.Exists(c => c.NameType.ToLower() == $"weight_win{radiostr}4".ToLower()))
                        {
                            var model = XChipAwb.Find(c => c.NameType.ToLower() == $"weight_win{radiostr}4".ToLower());
                            SetAWBRegister(model, Convert.ToUInt32($"{ weight_win4.Text}{weight_win5.Text}", 16));
                        }

                    }
                    break;
                default:
                    break;
            }
            bool isbool = awbChecked.Items[0].CheckState == CheckState.Checked;
            //重新画图
            ImageSetPoint(isbool);
        }

        private void IMG7_STARTX_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (isRead)//判断是否在读，读的时候不进行写
            {
                return;
            }
            var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            int tagNumber = 6;
            if (string.IsNullOrEmpty(txtEdit.Text)) return;
            ulong Val = 0;
            switch (txtEdit.Tag.ToString())
            {
                case "0":
                    {
                        if (XChipAwb.Exists(c => c.NameType.ToLower() == $"startX_win{radiostr}{tagNumber}".ToLower()))
                        {
                            var model = XChipAwb.Find(c => c.NameType.ToLower() == $"startX_win{radiostr}{tagNumber}".ToLower());
                            Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                            SetAWBRegister(model, Val);
                        }

                    }
                    break;
                case "1":
                    {
                        if (XChipAwb.Exists(c => c.NameType.ToLower() == $"startY_win{radiostr}{tagNumber}".ToLower()))
                        {
                            var model = XChipAwb.Find(c => c.NameType.ToLower() == $"startY_win{radiostr}{tagNumber}".ToLower());
                            Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                            SetAWBRegister(model, Val);
                        }

                    }
                    break;
                case "2":
                    {
                        if (XChipAwb.Exists(c => c.NameType.ToLower() == $"endX_win{radiostr}{tagNumber}".ToLower()))
                        {
                            var model = XChipAwb.Find(c => c.NameType.ToLower() == $"endX_win{radiostr}{tagNumber}".ToLower());
                            Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                            SetAWBRegister(model, Val);
                        }

                    }
                    break;
                case "3":
                    {
                        if (XChipAwb.Exists(c => c.NameType.ToLower() == $"endY_win{radiostr}{tagNumber}".ToLower()))
                        {
                            var model = XChipAwb.Find(c => c.NameType.ToLower() == $"endY_win{radiostr}{tagNumber}".ToLower());
                            Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                            SetAWBRegister(model, Val);
                        }

                    }
                    break;
                case "4":
                    {
                        if (string.IsNullOrEmpty(weight_win7.Text)) return;//两个框组成一个寄存器，所以下发时还需判断另一个框是否为空
                        if (XChipAwb.Exists(c => c.NameType.ToLower() == $"weight_win{radiostr}6".ToLower()))
                        {
                            var model = XChipAwb.Find(c => c.NameType.ToLower() == $"weight_win{radiostr}6".ToLower());
                            SetAWBRegister(model, Convert.ToUInt32($"{ weight_win6.Text}{weight_win7.Text}", 16));
                        }

                    }
                    break;
                default:
                    break;
            }
            bool isbool = awbChecked.Items[0].CheckState == CheckState.Checked;
            //重新画图
            ImageSetPoint(isbool);
        }

        private void IMG8_STARTX_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (isRead)//判断是否在读，读的时候不进行写
            {
                return;
            }
            var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            int tagNumber = 7;
            if (string.IsNullOrEmpty(txtEdit.Text)) return;
            ulong Val = 0;
            switch (txtEdit.Tag.ToString())
            {
                case "0":
                    {
                        if (XChipAwb.Exists(c => c.NameType.ToLower() == $"startX_win{radiostr}{tagNumber}".ToLower()))
                        {
                            var model = XChipAwb.Find(c => c.NameType.ToLower() == $"startX_win{radiostr}{tagNumber}".ToLower());
                            Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                            SetAWBRegister(model, Val);
                        }

                    }
                    break;
                case "1":
                    {
                        if (XChipAwb.Exists(c => c.NameType.ToLower() == $"startY_win{radiostr}{tagNumber}".ToLower()))
                        {
                            var model = XChipAwb.Find(c => c.NameType.ToLower() == $"startY_win{radiostr}{tagNumber}".ToLower());
                            Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                            SetAWBRegister(model, Val);
                        }

                    }
                    break;
                case "2":
                    {
                        if (XChipAwb.Exists(c => c.NameType.ToLower() == $"endX_win{radiostr}{tagNumber}".ToLower()))
                        {
                            var model = XChipAwb.Find(c => c.NameType.ToLower() == $"endX_win{radiostr}{tagNumber}".ToLower());
                            Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                            SetAWBRegister(model, Val);
                        }

                    }
                    break;
                case "3":
                    {
                        if (XChipAwb.Exists(c => c.NameType.ToLower() == $"endY_win{radiostr}{tagNumber}".ToLower()))
                        {
                            var model = XChipAwb.Find(c => c.NameType.ToLower() == $"endY_win{radiostr}{tagNumber}".ToLower());
                            Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                            SetAWBRegister(model, Val);
                        }

                    }
                    break;
                case "4":
                    {
                        if (string.IsNullOrEmpty(weight_win6.Text)) return;//两个框组成一个寄存器，所以下发时还需判断另一个框是否为空
                        if (XChipAwb.Exists(c => c.NameType.ToLower() == $"weight_win{radiostr}6".ToLower()))
                        {
                            var model = XChipAwb.Find(c => c.NameType.ToLower() == $"weight_win{radiostr}6".ToLower());
                            SetAWBRegister(model, Convert.ToUInt32($"{ weight_win6.Text}{weight_win7.Text}", 16));
                        }

                    }
                    break;
                default:
                    break;
            }
            bool isbool = awbChecked.Items[0].CheckState == CheckState.Checked;
            //重新画图
            ImageSetPoint(isbool);
        }

        private void AWB_BLC_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                AWB_BLC_Leave(sender, null);
            }
        }

        private void AWB_MAXVALUE_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                AWB_MAXVALUE_Leave(sender, null);
            }
        }

        private void AWB_MINVALUE_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                AWB_MINVALUE_Leave(sender, null);
            }
        }

        

        private void IMG1_STARTX_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                IMG1_STARTX_Leave(sender, null);
            }
        }

      

        private void simpleButton4_Click(object sender, EventArgs e)
        {
            ////SerialSend.isDebug = false;
            //SaveFileDialog sfd = new SaveFileDialog();
            ////设置文件类型 
            //sfd.Filter = "配置文件（*.txt）|*.txt";

            ////设置默认文件类型显示顺序 
            //sfd.FilterIndex = 1;

            ////保存对话框是否记忆上次打开的目录 
            //sfd.RestoreDirectory = true;
            //sfd.FileName = "IQ_Setting_AWB_" + DateTime.Now.ToString("yyyyMMddHHmmss");
            //if (sfd.ShowDialog() == DialogResult.OK)
            //{
            //    ////StringBuilder sb = new StringBuilder();
            //    var Pe_dal = new parameter_exportManager();
            //    if (null != Pe_dal)
            //    {
            //        WaitBefore.Start("参数导出", "正在加载中", 60);

            //        var tempList = Pe_dal.GetListToExport().Where(c => c.Module == "AWB").OrderBy(c => c.SortNum).ToList();
         
            //        #region 判断是否需要用白天的数据
            //        var Pe_patch_dal = new parameter_export_patchManager();
            //        var patchList = Pe_patch_dal.GetList();
            //        var conditionList = tempList.Select(c => c.ConditionAddr).Distinct();
            //        foreach (var item in conditionList)
            //        {
            //            if (SerialSend.ReadRegister(Convert.ToUInt32(item, 16), sPortTemp) == 0)
            //            {
            //                patchList.RemoveAll(c => c.ConditionAddr == item);
            //            }
            //        }
            //        #endregion

            //        var sb =   XChipHelp.ReadParameterExportExpand(tempList, "80", patchList, sPortTemp);
            //        //_veryshort_short_long
            //        var awblist = XChipAwb.Where(c => c.NameType.Contains("_long")).ToList();
                    

            //        System.IO.File.WriteAllText(sfd.FileName, sb.ToString());
            //        WaitBefore.Close();
            //        DevExpress.XtraEditors.XtraMessageBox.Show("参数导出完成");
            //    }
            //    //SerialSend.isDebug = true;
            //}
        }

        private void simpleButton6_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread(threadPro);
            thread.Start();
        }
        private void threadPro()

        {

            MethodInvoker methodInvoker = new MethodInvoker(ShowFormB);

            BeginInvoke(methodInvoker);

        }

        private void ShowFormB()

        {
            //string str = "\t      \t 在不同色温下对着灰卡，勾选“Debug”，点击“Statistics”，红点位置即为大致统计出的全局白点数量所对应的平均位置，将红点位置框入色温框中，作为该色温下的白点标定框；注意色温框需要有交集且若某个窗口的 Weight 值为 0 后，则后面的色温窗口框不再生效。\r\n"
            //   + "\t      \t AWB Move使能后，可通过计算当前的awb_color色温值与CT_A、CT_C、CT_D的设定阈值进行比较，对该色温下的白平衡B_gain与R_gain进行偏移补偿，其中：\r\n"
            //   + "\t      \t if(awb_color ≥CT_D)则补偿CT_D色温的offset设定值；if(awb_color ≤CT_A)则补偿CT_A色温的offset设定值；中间色温则进行相邻offset设定值的插值补偿。\r\n"
            //   + "\t      \t 通过统计Win_redlight色温框的Red pixel与Red_pixel_num_thr设定阈值进行比较，用于判断是否处于redlight mode。\r\n"
            //   + "\t      \t 更多详见《调试工具用户手册》。";
            string str = "";
            XChip_OptimizationTips ot = new XChip_OptimizationTips("AWB调优Tips", str);
            ot.StartPosition = FormStartPosition.CenterScreen;
            //ot.ShowDialog();
            ot.Show();

        }

        private async void chk_nAWBRedLightAdjEn_CheckedChanged(object sender, EventArgs e)
        {
            if (isRead) return;

            if (XChipAwb.Exists(c => c.NameType.ToLower() == $"nAWBRedLightAdj_En".ToLower()))
            {
                var model = XChipAwb.Find(c => c.NameType.ToLower() == $"nAWBRedLightAdj_En".ToLower());
                var Add = await ReadRegisterAsync(model.Address);
                var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), nAWBRedLightAdj_En.Checked);
                SetAWBRegister(model, Val);
            }

        }

        private void Win_red_light_keyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter)
            {
                TextEdit txt = sender as TextEdit;
                if (string.IsNullOrEmpty(txt.Text)) return;

                switch (txt.Tag.ToString())
                {
                    case "0":
                        if (XChipAwb.Exists(c => c.NameType.ToLower() == $"redlight_win_start_x{radiostr}".ToLower()))
                        {
                            var model = XChipAwb.Find(c => c.NameType.ToLower() == $"redlight_win_start_x{radiostr}".ToLower());
                            var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                            SetAWBRegister(model, Val);
                        }

                        break;
                    case "1":
                        if (XChipAwb.Exists(c => c.NameType.ToLower() == $"redlight_win_start_y{radiostr}".ToLower()))
                        {
                            var model = XChipAwb.Find(c => c.NameType.ToLower() == $"redlight_win_start_y{radiostr}".ToLower());
                            var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                            SetAWBRegister(model, Val);
                        }

                        break;
                    case "2":
                        if (XChipAwb.Exists(c => c.NameType.ToLower() == $"redlight_win_end_x{radiostr}".ToLower()))
                        {
                            var model = XChipAwb.Find(c => c.NameType.ToLower() == $"redlight_win_end_x{radiostr}".ToLower());
                            var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                            SetAWBRegister(model, Val);
                        }

                        break;
                    case "3":
                        if (XChipAwb.Exists(c => c.NameType.ToLower() == $"redlight_win_end_y{radiostr}".ToLower()))
                        {
                            var model = XChipAwb.Find(c => c.NameType.ToLower() == $"redlight_win_end_y{radiostr}".ToLower());
                            var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                            SetAWBRegister(model, Val);
                        }

                        break;
                    default:
                        break;
                }
            }
            bool isbool = awbChecked.Items[0].CheckState == CheckState.Checked;
            //重新画图
            ImageSetPoint(isbool);
        }

        private void txt_nAWBRedLightThr_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
                if (string.IsNullOrEmpty(txtEdit.Text)) return;
               
                if (XChipAwb.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
                {
                    var model = XChipAwb.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Red_pixel_num_thr.Text);
                    SetAWBRegister(model, Val);
                }
                if (XChipAwb.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
                {
                    var model = XChipAwb.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Red_pixel_num_thr.Text);
                    SetAWBRegister(model, Val);
                }

            }
        }

        private void txt_nAWBRedLightGainThr_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
                if (string.IsNullOrEmpty(txtEdit.Text)) return;
                
                if (XChipAwb.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
                {
                    var model = XChipAwb.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Gain_thr.Text);
                    SetAWBRegister(model, Val);
                }
                if (XChipAwb.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
                {
                    var model = XChipAwb.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Gain_thr.Text);
                    SetAWBRegister(model, Val);
                }

            }
        }

        private async void simpleButton7_Click(object sender, EventArgs e)
        {
            isRead = true;
            List<string> listfiltration = new List<string> { "Red_pixel_num_thr_h", "Red_pixel_num_thr_l", "Gain_thr_H", "Gain_thr_L", "cur_Red_pixel_num_H", "cur_Red_pixel_num_L", "nAWBRedLightAdj_En" };
            List<string> NameList = new List<string>();
            // 记录开始时间和寄存器计数
            var startTime = DateTime.Now;
            int totalRegisterCount = 0;
            for (int i = 0; i < XChipAwb.Count; i++)
            {
                var type = AppHelper.NameTypeToString(XChipAwb[i].NameType);
                //加上单选框的_en
                if (XChipAwb[i].ControlType == "1")
                {
                    type = XChipAwb[i].NameType;
                }
                var col = this.Controls.Find(type, true);
                foreach (var item in col)
                {
                    if (NameList.Exists(c => c.Contains(type)))
                    {
                        continue;
                    }
                    else
                    {

                        //过滤掉指定寄存器
                        if (!listfiltration.Exists(c => c.Contains(item.Name.ToString())))
                        {
                            continue;
                        }
                        if (item is TextEdit)
                        {

                            var nList = XChipAwb.Where(c => c.NameType.ToLower().Contains(item.Name.ToString().ToLower()) && c.ControlType == "0").ToList();
                            if (nList.Count == 1)
                            {
                                // 单个寄存器读取
                                var address = nList[0].Address;
                                totalRegisterCount++;
                                await ReadRegisterAndUpdateTextAsync(address, item as TextEdit, nList[0].Bits, nList[0].Hex);
                            }
                            else
                            {
                                // 多个寄存器组合读取
                                totalRegisterCount += nList.Count;
                                await ReadMultipleRegistersAndUpdateTextAsync(nList, item as TextEdit);
                            }
                        }
                        else if (item is CheckEdit)
                        {
                            var nList = XChipAwb.Where(c => c.NameType.ToLower().Contains(item.Name.ToString().ToLower()) && c.ControlType == "1").ToList();
                            if (nList.Count == 1)
                            {
                                var address = nList[0].Address;
                                totalRegisterCount++;
                                await ReadRegisterAndUpdateCheckAsync(address, item as CheckEdit, nList[0].Bits, nList[0].Hex);
                            }
                        }


                    }
                }
                NameList.Add(type);
            }
            isRead = false;
        }

        private void txt_cur_Red_pixel_num_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
                if (string.IsNullOrEmpty(txtEdit.Text)) return;
                if (XChipAwb.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
                {
                    var model = XChipAwb.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Red_pixel_num_thr.Text);
                    SetAWBRegister(model, Val);
                }
                if (XChipAwb.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
                {
                    var model = XChipAwb.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Red_pixel_num_thr.Text);
                    SetAWBRegister(model, Val);
                }

            }
        }

        private void simpleButton5_MouseEnter(object sender, EventArgs e)
        {
            isWrite = true;
        }

        private void simpleButton5_MouseLeave(object sender, EventArgs e)
        {
            isWrite = false;
        }

        private void AWB_radiomode_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (isRead) return;
            var aeradio = sender as DevExpress.XtraEditors.RadioGroup;
            if (aeradio.EditValue.ToString() == "1")
            {
                radiostr = "_long";
            }
            else if (aeradio.EditValue.ToString() == "2")
            {
                radiostr = "_short";
            }
            else if (aeradio.EditValue.ToString() == "3")
            {
                radiostr = "_veryshort";
            }
        }

        private void weight_win0_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                weight_win0_Leave(sender, null);
            }
        }

        private void weight_win0_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (isRead)//判断是否在读，读的时候不进行写
            {
                return;
            }
            //var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            if (string.IsNullOrEmpty(weight_win0.Text)||string.IsNullOrEmpty(weight_win1.Text)) return;

            if (XChipAwb.Exists(c => c.NameType.ToLower() == $"{weight_win0.Name}{radiostr}".ToLower()))
            {
                var model = XChipAwb.Find(c => c.NameType.ToLower() == $"{weight_win0.Name}{radiostr}".ToLower());
                var Val =Convert.ToUInt32 (weight_win0.Text+ weight_win1.Text,16);
                SetAWBRegister(model, Val);
            }
            bool isbool = awbChecked.Items[0].CheckState == CheckState.Checked;
            //重新画图
            ImageSetPoint(isbool);
        }

        private void weight_win2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                weight_win2_Leave(sender, null);
            }
        }

        private void weight_win2_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (isRead)//判断是否在读，读的时候不进行写
            {
                return;
            }
            //var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            if (string.IsNullOrEmpty(weight_win2.Text) || string.IsNullOrEmpty(weight_win3.Text)) return;

            if (XChipAwb.Exists(c => c.NameType.ToLower() == $"{weight_win2.Name}{radiostr}".ToLower()))
            {
                var model = XChipAwb.Find(c => c.NameType.ToLower() == $"{weight_win2.Name}{radiostr}".ToLower());
                var Val = Convert.ToUInt32(weight_win2.Text + weight_win3.Text,16);
                SetAWBRegister(model, Val);
            }
            bool isbool = awbChecked.Items[0].CheckState == CheckState.Checked;
            //重新画图
            ImageSetPoint(isbool);
        }

        private void weight_win4_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                weight_win4_Leave(sender, null);
            }
        }

        private void weight_win4_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (isRead)//判断是否在读，读的时候不进行写
            {
                return;
            }
            //var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            if (string.IsNullOrEmpty(weight_win4.Text) || string.IsNullOrEmpty(weight_win5.Text)) return;

            if (XChipAwb.Exists(c => c.NameType.ToLower() == $"{weight_win4.Name}{radiostr}".ToLower()))
            {
                var model = XChipAwb.Find(c => c.NameType.ToLower() == $"{weight_win4.Name}{radiostr}".ToLower());
                var Val = Convert.ToUInt32(weight_win4.Text + weight_win5.Text,16);
                SetAWBRegister(model, Val);
            }
            bool isbool = awbChecked.Items[0].CheckState == CheckState.Checked;
            //重新画图
            ImageSetPoint(isbool);
        }

        private void weight_win6_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                weight_win6_Leave(sender, null);
            }
        }

        private void weight_win6_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (isRead)//判断是否在读，读的时候不进行写
            {
                return;
            }
            //var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            if (string.IsNullOrEmpty(weight_win6.Text) || string.IsNullOrEmpty(weight_win7.Text)) return;

            if (XChipAwb.Exists(c => c.NameType.ToLower() == $"{weight_win6.Name}{radiostr}".ToLower()))
            {
                var model = XChipAwb.Find(c => c.NameType.ToLower() == $"{weight_win6.Name}{radiostr}".ToLower());
                var Val = Convert.ToUInt32(weight_win6.Text + weight_win7.Text,16);
                SetAWBRegister(model, Val);
            }
            bool isbool = awbChecked.Items[0].CheckState == CheckState.Checked;
            //重新画图
            ImageSetPoint(isbool);
        }

        private void Gain_thr_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            txt_nAWBRedLightGainThr_KeyDown(sender, keyEnter);
        }

        private void Red_pixel_num_thr_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            txt_nAWBRedLightThr_KeyDown(sender, keyEnter);
        }
        /// <summary>
        /// 异步读取单个寄存器并更新文本框
        /// </summary>
        private async Task ReadRegisterAndUpdateTextAsync(string address, TextEdit textEdit, string bits, string hex)
        {
            try
            {
                Console.WriteLine($"[ADBForm] 开始读取寄存器: {address}");

                // 发送读取命令
                string adbCommand = $"adb shell setprop vendor.extcamera.xxw.ispregctrl \"0,{address},0,0\"";
                adbService.ExecuteAdbCommandAsync(adbCommand, $"读操作 - 地址:{address}");

                // 等待命令执行
                await Task.Delay(10);

                // 获取寄存器值
                var record = await Task.Run(() => LogcatDataManager.FindByRegisterAddressSync(address, "Read", 500));

                if (record != null && !string.IsNullOrEmpty(record.Values) && record.Values != "0")
                {
                    string processedValue = record.Values;
                    if (processedValue.StartsWith("0x"))
                    {
                        processedValue = processedValue.Substring(2);
                    }

                    Console.WriteLine($"[ADBForm] 成功读取寄存器 {address} 值: {processedValue}");

                    // 处理位操作
                    string displayValue;
                    if (processedValue != "0")
                    {
                        var uintValue = BitsHelp.GetBitsToUint(bits, hex, processedValue);
                        displayValue = uintValue.ToString("X");
                        Console.WriteLine($"[ADBForm] 寄存器 {address} Bits处理后值: {displayValue} (bits:{bits}, hex:{hex})");
                    }
                    else
                    {
                        displayValue = "0";
                    }

                    // 更新UI
                    UpdateTextEdit(textEdit, displayValue);
                }
                else
                {
                    Console.WriteLine($"[ADBForm] 读取寄存器 {address} 失败，值为空或0，使用默认值0");
                    UpdateTextEdit(textEdit, "0");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"[ADBForm] 读取寄存器 {address} 异常: {ex.Message}");
                UpdateTextEdit(textEdit, "错误");
            }
        }

        /// <summary>
        /// 异步读取多个寄存器并组合更新文本框
        /// </summary>
        private async Task ReadMultipleRegistersAndUpdateTextAsync(List<addressregister> nList, TextEdit textEdit)
        {
            try
            {
                Console.WriteLine($"[ADBForm] 开始读取多个寄存器，数量: {nList.Count}");

                string combinedValue = string.Empty;

                // 按预定义的优先级顺序读取寄存器
                var priorityOrder = new[] { "_h", "_m0", "_m1", "_m2", "_m3", "_l" };

                foreach (var priority in priorityOrder)
                {
                    addressregister register = FindRegisterByPriority(nList, priority);

                    if (register != null)
                    {
                        Console.WriteLine($"[ADBForm] 读取优先级 '{priority}' 的寄存器: {register.NameType} ({register.Address})");

                        // 发送读取命令
                        string adbCommand = $"adb shell setprop vendor.extcamera.xxw.ispregctrl \"0,{register.Address},0,0\"";
                        adbService.ExecuteAdbCommandAsync(adbCommand, $"读操作 - 地址:{register.Address}");

                        // 等待命令执行
                        await Task.Delay(10);

                        // 获取寄存器值
                        var record = await Task.Run(() => LogcatDataManager.FindByRegisterAddressSync(register.Address, "Read", 500));

                        if (record != null && !string.IsNullOrEmpty(record.Values) && record.Values != "0")
                        {
                            string processedValue = record.Values;
                            if (processedValue.StartsWith("0x"))
                            {
                                processedValue = processedValue.Substring(2);
                            }

                            combinedValue += processedValue.PadLeft(2, '0');
                            Console.WriteLine($"[ADBForm] 成功读取 {register.Address}，当前组合值: {combinedValue}");
                        }
                        else
                        {
                            combinedValue += "00";
                            Console.WriteLine($"[ADBForm] 读取 {register.Address} 失败，使用默认值00，当前组合值: {combinedValue}");
                        }
                    }
                    else
                    {
                        Console.WriteLine($"[ADBForm] 未找到包含 '{priority}' 的寄存器");
                    }
                }

                Console.WriteLine($"[ADBForm] 最终组合值: {combinedValue}");

                // 更新UI
                UpdateTextEdit(textEdit, combinedValue);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"[ADBForm] 读取多个寄存器失败: {ex.Message}");
                UpdateTextEdit(textEdit, "错误");
            }
        }

        /// <summary>
        /// 根据优先级查找寄存器
        /// </summary>
        private addressregister FindRegisterByPriority(List<addressregister> nList, string priority)
        {
            if (priority == "_l")
            {
                // 特殊处理 _l 寄存器
                if (nList.Exists(c => c.NameType.ToLower().Contains("_l")))
                {
                    // 首先查找以 "_l" 结尾的寄存器
                    var exactMatch = nList.Find(c => c.NameType.ToLower().EndsWith("_l"));
                    if (exactMatch != null)
                    {
                        Console.WriteLine($"[ADBForm] 找到以 '_l' 结尾的寄存器: {exactMatch.NameType}");
                        return exactMatch;
                    }
                    else
                    {
                        // 如果没有以 "_l" 结尾的，则使用最后一个包含 "_l" 的寄存器
                        var containsMatch = nList.Last(c => c.NameType.ToLower().Contains("_l"));
                        Console.WriteLine($"[ADBForm] 找到包含 '_l' 的寄存器: {containsMatch.NameType}");
                        return containsMatch;
                    }
                }
            }
            else
            {
                // 其他寄存器正常处理
                var register = nList.Find(c => c.NameType.ToLower().Contains(priority));
                if (register != null)
                {
                    Console.WriteLine($"[ADBForm] 找到包含 '{priority}' 的寄存器: {register.NameType}");
                    return register;
                }
            }

            return null;
        }

        /// <summary>
        /// 更新文本框的辅助方法
        /// </summary>
        private void UpdateTextEdit(TextEdit textEdit, string value)
        {
            if (textEdit.InvokeRequired)
            {
                textEdit.Invoke(new Action(() =>
                {
                    textEdit.Text = value;
                    Console.WriteLine($"[ADBForm] UI已更新，文本框值: {value}");
                }));
            }
            else
            {
                textEdit.Text = value;
                Console.WriteLine($"[ADBForm] UI已更新，文本框值: {value}");
            }
        }

        /// <summary>
        /// 异步读取寄存器并更新复选框
        /// </summary>
        private async Task ReadRegisterAndUpdateCheckAsync(string address, CheckEdit checkEdit, string bits, string hex)
        {
            try
            {
                Console.WriteLine($"[ADBForm] 开始读取寄存器并更新复选框: {address}");

                string adbCommand = $"adb shell setprop vendor.extcamera.xxw.ispregctrl \"0,{address},0,0\"";
                adbService.ExecuteAdbCommandAsync(adbCommand, $"读操作 - 地址:{address}");

                // 等待命令执行
                await Task.Delay(10);

                var record = await Task.Run(() => LogcatDataManager.FindByRegisterAddressSync(address, "Read", 500));

                bool isChecked = false;
                if (!string.IsNullOrEmpty(record.Values) && record.Values != "0")
                {
                    isChecked = BitsHelp.GetBitsCheckedToBool(bits, hex, record.Values) == 0;
                    Console.WriteLine($"[ADBForm] 复选框更新 - 地址:{address}, 值:{record.Values}, 状态:{isChecked}");
                }
                else
                {
                    Console.WriteLine($"[ADBForm] 复选框更新 - 地址:{address}, 值无效, 状态:false");
                }

                // 更新UI
                if (checkEdit.InvokeRequired)
                {
                    checkEdit.Invoke(new Action(() => checkEdit.Checked = isChecked));
                }
                else
                {
                    checkEdit.Checked = isChecked;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"[ADBForm] 读取寄存器 {address} 更新复选框失败: {ex.Message}");
                if (checkEdit.InvokeRequired)
                {
                    checkEdit.Invoke(new Action(() => checkEdit.Checked = false));
                }
                else
                {
                    checkEdit.Checked = false;
                }
            }
        }

        /// <summary>
        /// 异步读取单个寄存器返回值
        /// </summary>
        private async Task<long> ReadRegisterAsync(string address)
        {
            try
            {
                Console.WriteLine($"[ADBForm] 开始读取寄存器: {address}");
                // 发送读取命令
                string adbCommand = $"adb shell setprop vendor.extcamera.xxw.ispregctrl \"0,{address},0,0\"";
                adbService.ExecuteAdbCommandAsync(adbCommand, $"读操作 - 地址:{address}");

                // 等待命令执行
                await Task.Delay(10);

                // 获取寄存器值
                var record = await Task.Run(() => LogcatDataManager.FindByRegisterAddressSync(address, "Read", 500));

                if (record != null && !string.IsNullOrEmpty(record.Values) && record.Values != "0")
                {
                    return Convert.ToInt32(record.Values, 16);
                }
                else
                {
                    Console.WriteLine($"[ADBForm] 读取寄存器 {address} 失败，值为空或0，使用默认值0");
                    return 0;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"[ADBForm] 读取寄存器 {address} 异常: {ex.Message}");
                return 0;
            }
        }
        /// <summary>
        /// 异步写入单个寄存器，在值不匹配时自动重试一次
        /// </summary>
        private async Task<bool> WriteRegisterAsync(string address, string value)
        {
            int maxRetries = 1; // 值不匹配时最多重试一次
            int retryCount = 0;

            while (retryCount <= maxRetries)
            {
                try
                {
                    if (retryCount > 0)
                    {
                        Console.WriteLine($"[ADBForm] 第 {retryCount} 次重试写入寄存器: {address} 值: {value}");
                    }
                    else
                    {
                        Console.WriteLine($"[ADBForm] 开始写入寄存器: {address} 值: {value}");
                    }

                    // 处理位操作（如果有）
                    string processedValue = value;

                    // 确保值是十六进制格式（不带0x前缀）
                    if (processedValue.StartsWith("0x"))
                    {
                        processedValue = processedValue.Substring(2);
                    }

                    // 发送写入命令
                    string adbCommand = $"adb shell setprop vendor.extcamera.xxw.ispregctrl \"1,{address},{processedValue},0\"";
                    adbService.ExecuteAdbCommandAsync(adbCommand, $"写操作 - 地址:{address} 值:{processedValue}");

                    // 等待命令执行
                    await Task.Delay(10);

                    // 验证写入是否成功 - 等待写入记录出现
                    var writeRecord = await Task.Run(() => LogcatDataManager.FindByRegisterAddressSync(address, "Write", 500));

                    if (writeRecord != null && !string.IsNullOrEmpty(writeRecord.Values))
                    {
                        string actualValue = writeRecord.Values;
                        if (actualValue.StartsWith("0x"))
                        {
                            actualValue = actualValue.Substring(2);
                        }

                        // 比较写入的值是否匹配
                        bool success = string.Equals(actualValue, processedValue, StringComparison.OrdinalIgnoreCase);

                        if (success)
                        {
                            Console.WriteLine($"[ADBForm] 成功写入寄存器 {address} 值: {processedValue}");
                            return true;
                        }
                        else
                        {
                            Console.WriteLine($"[ADBForm] 写入寄存器 {address} 值不匹配，期望: {processedValue}，实际: {actualValue}");

                            // 如果不匹配且还有重试次数，则继续重试
                            if (retryCount < maxRetries)
                            {
                                retryCount++;
                                Console.WriteLine($"[ADBForm] 准备重试写入...");
                                continue; // 直接进入下一次循环，重新发送命令
                            }
                            else
                            {
                                Console.WriteLine($"[ADBForm] 写入寄存器 {address} 重试次数已用完，仍然不匹配");
                                return false;
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine($"[ADBForm] 写入寄存器 {address} 失败，未找到写入确认记录");

                        // 如果未找到记录且还有重试次数，则重试
                        if (retryCount < maxRetries)
                        {
                            retryCount++;
                            Console.WriteLine($"[ADBForm] 准备重试写入...");
                            continue;
                        }
                        else
                        {
                            return false;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"[ADBForm] 写入寄存器 {address} 异常: {ex.Message}");

                    // 如果异常且还有重试次数，则重试
                    if (retryCount < maxRetries)
                    {
                        retryCount++;
                        Console.WriteLine($"[ADBForm] 准备重试写入...");
                        await Task.Delay(10); // 异常后稍作延迟再重试
                        continue;
                    }
                    else
                    {
                        return false;
                    }
                }
            }

            return false; // 理论上不会执行到这里
        }
    }
}
